//导入必要的模块
const fs = require("fs");
const path = require('path');
const { execSync } = require("child_process");
// 脚本规则:把脚本放到要整改文件的目录下,然后再cmd窗口中执行:node modifyXTS.js -1 1。
// 定义要搜索的根目录(修改为你的目标路径)
const currentPath = __dirname;
const args = process.argv.slice(2);//命令中从第三个参数开始获取
let level = '';
let Standard1 = '';
let Standard2 = '';
function processFile(filePath, newParam, fileType) {
    console.error(`filePath:`, filePath);
    try {
        let content = fs.readFileSync(filePath, 'utf8');
        lines = content.split(/\r?\n/).filter(Boolean);
        for (let i = 0; i < lines.length; i++) {
            // 正则表达式匹配HWTEST的第三个参数
            if (fileType === 'cpp') {
                if (lines[i].includes('HWTEST_F') && lines[i].includes('Function | MediumTest | Level')) {
                    continue;
                }
                if (!lines[i].includes('HWTEST_F(')) {
                    continue;
                }
                const regex =/(HWTEST_F\s*\(\s*[^,]+?,\s*[^,]+?,\s*)([^\)]+)(.*)/g;
                let thirdParameter = regex.exec(lines[i]);

                if ((thirdParameter !== null) && ((!thirdParameter[2].includes('0')) )) {
                    console.log(`文件中原测试用例等级非0, 建议用户再手动检查下`, filePath);
                }
                if ((thirdParameter == null) && ((!lines[i+1].includes('0')) )) {
                    console.log(`文件中原测试用例等级非0, 建议用户再手动检查下`, filePath);
                }
                const newContent = lines[i].replace(regex, `$1${Standard1 + 'Level' + newParam}$3`);
                if (newContent !== lines[i]) {
                    lines[i] = newContent;
                    const updatedContent = lines.join('\n');
                    fs.writeFileSync(filePath, updatedContent, 'utf8');
                }
            } else if (fileType === 'ets') {
                if (lines[i].includes('it') && lines[i].includes('TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL')) {
                    continue;
                }
                if (!lines[i].includes('it(') || !lines[i].includes('async')) {
                    continue;
                }
                const regex = /^([^,]*),([^,]*)/;
                const match = lines[i].match(regex);
                const firstPart = match[1];
                const secondPart = match[2];
                if ((match !== null) && (!secondPart.includes('0'))){
                    console.log(`文件中原测试用例等级非0, 建议用户再手动检查下`, filePath);
                }
                const newContent1 = `${firstPart},${' Level.LEVEL' + newParam},${lines[i].split(',')[2] + lines[i].slice(-1)}`;
                if (newContent1 !== lines[i]) {
                    lines[i] = newContent1;
                    const updatedContent1 = lines.join('\n');
                    fs.writeFileSync(filePath, updatedContent1, 'utf8');
                }
            }
        }
    } catch (error) {
        console.error(`处理文件时出错 ${filePath}: ${error}`)
    }
}
function traverseDirectory(dirPath, newParam) {
    try {
        const entries = fs.readdirSync(dirPath, { withFileTypes: true });
        for (const entry of entries) {
            const fullPath = path.join(dirPath, entry.name);
            if (fullPath.includes('modifyXTS-v2')){
                continue;
            }
            if (entry.isDirectory()){
                traverseDirectory(fullPath, newParam);
            } else if (path.extname(entry.name) === '.cpp') {//extname获取文件扩展名
                processFile(fullPath, newParam, 'cpp');
            } else if (path.extname(entry.name) === '.ets' || path.extname(entry.name) === '.js') {//extname获取文件扩展名
                processFile(fullPath, newParam, 'ets');
            }
        }
    } catch (error) {
        console.error(`遍历目录时出错 ${dirPath}: ${error}`);
    }
}
let string = 'it(testbadgeClassPropNone, Level.LEVEL4) {';
function main() {
    for (let i = 0; i < args.length; i++) {
        if (args[i] === '-L') {
            // 找到`-l`后面的参数
            if (i + 1 < args.length) {
                level = args[i + 1];
            } else {
                console.error('Error: Missing level parameter after -L');
                process.exit(1);
            }
        } else if (args[i] === '-A') {
            if (i + 1 < args.length) {
                Standard2 = 'TestType.FUNCTION | Size.MEDIUMTEST | ';
                Standard1 = 'Function | MediumTest | ';
            } else {
                console.error('Error: Missing level parameter after -A');
                process.exit(1);
            }
        }
    }
    traverseDirectory(currentPath, level);
    console.log('处理完成');
}
main();